/**  
 * @Title: CmpFetchDataService.java
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-05-06 14:23:02 
 */
package com.fsxgt.datagrid.component.service.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fsxgt.datagrid.component.bo.CmpFetchDataBO;
import com.fsxgt.datagrid.component.bo.CmpFetchDataColumnBO;
import com.fsxgt.datagrid.component.dao.CmpFetchDataColumnMapper;
import com.fsxgt.datagrid.component.dao.CmpFetchDataMapper;
import com.fsxgt.datagrid.ds.bo.DatasourceJdbcBO;
import com.fsxgt.datagrid.ds.component.QueryComponent;
import com.fsxgt.datagrid.ds.handler.DbHandler;
import com.fsxgt.datagrid.ds.service.impl.DatasourceJdbcService;
import com.fsxgt.datagrid.ds.service.impl.MetaService;
import com.fsxgt.datagrid.flow.dao.FlowInfoMapper;
import com.fsxgt.datagrid.flow.exception.FlowException;
import com.fsxgt.datagrid.flow.service.IComponentService;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: CmpFetchDataService
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-05-06 14:23:02
 */
@Slf4j
@Service
public class CmpFetchDataService implements IComponentService {

	@Autowired
	private FlowInfoMapper flowInfoMapper;

	@Autowired
	private CmpFetchDataMapper cmpFetchDataMapper;
	@Autowired
	private CmpFetchDataColumnMapper cmpFetchDataColumnMapper;
	@Autowired
	private DatasourceJdbcService datasourceJdbcService;
	@Autowired
	private QueryComponent queryComponent;
	@Autowired
	private MetaService metaService;

	@Transactional(rollbackFor = Exception.class)
	public void saveCmpFetchData(CmpFetchDataBO cmpFetchDataBO) throws ExecutionException, SQLException, FlowException {

//		validateSql(cmpFetchDataBO.getDatasourceId(), cmpFetchDataBO.getSqlText());

		CmpFetchDataBO bo = cmpFetchDataMapper.get(cmpFetchDataBO.getFlowInfoId(), cmpFetchDataBO.getComponentSeq());
		if (bo == null) {
			cmpFetchDataMapper.insert(cmpFetchDataBO);
		} else {
			cmpFetchDataMapper.update(cmpFetchDataBO);
		}

		for (CmpFetchDataColumnBO column : cmpFetchDataBO.getColumnList()) {
			column.setComponentSeq(cmpFetchDataBO.getComponentSeq());
			column.setFlowInfoId(cmpFetchDataBO.getFlowInfoId());
		}
		cmpFetchDataColumnMapper.delete(cmpFetchDataBO.getFlowInfoId(), cmpFetchDataBO.getComponentSeq());
		if (cmpFetchDataBO.getColumnList().size() > 0) {
			cmpFetchDataColumnMapper.insert(cmpFetchDataBO.getColumnList());
		}

		flowInfoMapper.updateFlowComponentName(cmpFetchDataBO.getName(), cmpFetchDataBO.getFlowInfoId(),
				cmpFetchDataBO.getComponentSeq());

	}

	@Transactional(rollbackFor = Exception.class)
	public void deleteComponent(CmpFetchDataBO cmpFetchDataBO) {
		deleteComponent(cmpFetchDataBO.getFlowInfoId(), cmpFetchDataBO.getComponentSeq());
	}

	/**
	 * @Title: deleteComponent
	 * @Description: TODO(描述)
	 * @param flowInfoId
	 * @param seq
	 * @see com.fsxgt.datagrid.core.service.impl.IComponentService#deleteComponent(java.lang.String,
	 *      java.lang.String)
	 * @author liuyi
	 * @date 2023-05-08 09:08:12
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteComponent(String flowInfoId, String seq) {
		// TODO Auto-generated method stub
		cmpFetchDataMapper.delete(flowInfoId, seq);
		cmpFetchDataColumnMapper.delete(flowInfoId, seq);
	}

	/**
	 * @Title: getComponentType
	 * @Description: TODO(描述)
	 * @return
	 * @see com.fsxgt.datagrid.core.service.impl.IComponentService#getComponentType()
	 * @author liuyi
	 * @date 2023-05-08 09:08:12
	 */
	@Override
	public String getComponentType() {
		// TODO Auto-generated method stub
		return "Fetch Data";
	}

	/**
	 * @Title: getSyncOffline
	 * @Description: TODO(描述)
	 * @param bo
	 * @author liuyi
	 * @date 2023-05-08 09:51:14
	 */
	public CmpFetchDataBO getFetchData(CmpFetchDataBO cmpFetchDataBO) {
		// TODO Auto-generated method stub
		CmpFetchDataBO bo = cmpFetchDataMapper.get(cmpFetchDataBO.getFlowInfoId(), cmpFetchDataBO.getComponentSeq());

		if (bo != null) {
			List<CmpFetchDataColumnBO> list = cmpFetchDataColumnMapper.getList(cmpFetchDataBO.getFlowInfoId(),
					cmpFetchDataBO.getComponentSeq());
			bo.setColumnList(list);
		}

		return bo;
	}

	/**
	 * @Title: getConfig
	 * @Description: TODO(描述)
	 * @param flowId
	 * @param seq
	 * @return
	 * @see com.fsxgt.datagrid.core.service.IComponentService#getConfig(java.lang.String,
	 *      java.lang.String)
	 * @author liuyi
	 * @date 2023-05-25 15:24:47
	 */
	@Override
	public Object getConfig(String flowId, String seq) {
		// TODO Auto-generated method stub

		CmpFetchDataBO bo = new CmpFetchDataBO();
		bo.setFlowInfoId(flowId);
		bo.setComponentSeq(seq);

		return getFetchData(bo);
	}

	


	


	

}
